﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Hud : MonoBehaviour {

	private float timeGlow = 0;

	public static int level = 1;

	private Transform selected = null;

	public static Hud instance = null;

	public int lemmingsCount = 3;
	public int summoned = 0;
	public int homed = 0;

	public Transform [] buildable;



	public int [] qty;

	private int [] startQty;

	private Color oldColor;

	private List<Transform> built = new List<Transform>();
	private List<Vector3> inicialPositions = new List<Vector3>();
	private List<Quaternion> inicialRotations = new List<Quaternion>();

	private Vector3 cameraVelocity = Vector3.zero;
	// Use this for initialization
	void Start () {
		startQty = (int[])qty.Clone();
		instance = this;
	}

	public void Advance() {
		Stop ();
		level += 1;
		Application.LoadLevel ("Level"+level); 
	}
	
	// Update is called once per frame
	void Update () {

		// mouse click
		if(Input.GetMouseButtonDown(0) && GUIUtility.hotControl == 0) {
		
			Vector3 inputPosition = Input.mousePosition; 
			Vector3 rayc = Camera.mainCamera.ScreenToWorldPoint(new Vector3(Screen.width - inputPosition.x, Screen.height - inputPosition.y, -10));
			Collider2D col = Physics2D.OverlapPoint(rayc);
			if(selected!=null) {
				selected.transform.position = new Vector3(rayc.x, rayc.y, selected.transform.position.z);
			}
			if(col!=null) {
				if(col.tag=="Buildable") {
					if(selected==null) {
						selected = col.transform;
					}
				}
			}

		}

		if(selected!=null) {
			if(Time.time > timeGlow) {
				if(selected.renderer.material.color==Color.white) {
					selected.renderer.material.color = oldColor;
				} else {
					selected.renderer.material.color = Color.white;
				}
				timeGlow = Time.time + 0.1f;
			}
		}
	}

	void Play() {
		inicialPositions = new List<Vector3>();
		inicialRotations = new List<Quaternion>();
		foreach(Transform t in built) {
			inicialPositions.Add (t.position);
			inicialRotations.Add (t.rotation);
		}

		Spawn.spawning = true;
		if(selected!=null) {
			if(selected.renderer.material.color != Color.white) {
				selected.renderer.material.color = Color.white;
			}
		}
		selected = null;
		foreach(Transform t in built) {
			t.rigidbody2D.isKinematic = false;
		}
	}
	

	void Stop() {
		Spawn.spawning = false;
		GameObject [] lemmings = GameObject.FindGameObjectsWithTag("Player");
		foreach(GameObject lemming in lemmings) {
			GameObject.Destroy(lemming);
		}
		for(int x = 0 ; x < built.Count ; x++) {
			Transform inMap = built[x];
			Vector3 inicialPos = inicialPositions[x];
			Quaternion inicialRot = inicialRotations[x];
			inMap.position = inicialPos;
			inMap.rotation = inicialRot;
			inMap.rigidbody2D.isKinematic = true;
		}
		//qty = (int[])startQty.Clone();
		selected = null;
		//built = new List<Transform>();
		summoned = 0;
		homed = 0;
	}


	void OnGUI() {

		GUI.Box(new Rect(0,0,Screen.width,70), GUIContent.none);

		GUI.Label (new Rect(10,45,80,80), "Fellas: "+lemmingsCount);
		GUI.Label (new Rect(100,45,100,80), "Summoned: "+summoned);
		GUI.Label (new Rect(210,45,80,80), "Safe: "+homed);

		if(!Spawn.spawning && selected==null) {
			int x = 0;
			int index = 0;
			foreach(Transform t in buildable) {
				int qtd = qty[index];
				if(qtd==0) continue;
				if (GUI.Button (new Rect (10, 10, 100, 30), t.name+" x"+qtd)) {
					Debug.Log (Screen.width);
					float xx = (Camera.main.transform.position.x+6.5f);
					float yy = (Camera.main.transform.position.y+6.5f);
					Object generated = Transform.Instantiate(t, new Vector3(xx, yy,0), Quaternion.identity);
					if(generated is Transform) {
						selected = (Transform)generated;
					}

					qty[index]--;
				}
				x+= 150;
				index++;
			}
		}

		if(selected==null) {
			if(!Spawn.spawning) {
				if (GUI.Button (new Rect (Screen.width-120, 10, 100, 30), "Play")) {
					Play ();
				}
			} else {
				if (GUI.Button (new Rect (Screen.width-120, 10, 100, 30), "Stop")) {
					Stop ();
				}
			}
		} else {
			if (GUI.Button (new Rect (Screen.width-120, 10, 100, 30), "Done")) {

				if(selected!=null) {
					if(selected.renderer.material.color != Color.white) {
						selected.renderer.material.color = Color.white;
					}
				}
				built.Add (selected);
				selected = null;
			}
			if (GUI.Button (new Rect (10, 10, 30, 30), "R+")) {
				selected.Rotate(new Vector3(0,0,-15));
			}
			if (GUI.Button (new Rect (70, 10, 30, 30), "R-")) {
				selected.Rotate(new Vector3(0,0,15));
			}
		}



	}
}
